package game;

import java.awt.Color;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import obj_file.Question;
import view.Home;
import view.New_Game;
import data.Question_File;
import view.Help_3;

public class Game {

	Question[] questions = new Question[18];
	int num_question = 0;
	int id_answer= 0;
	Question_File file = new Question_File();	
	int Score[] ={0,100,200,300,500,1000
				  ,2000,4000,8000,16000,32000
				  ,64000,125000,250000,500000,1000000};
	public int real_score;
	int num_score;
	static boolean choose_again = false;
	public Game() throws ParserConfigurationException, SAXException, IOException
	{
		questions=file.ChooseQuestion();	
		real_score = 0;
		num_score = 0 ;
	}
	
	public void Question_Output(int num_question ,JLabel lbl_Num_Question,
								JLabel lbl_Question,JLabel lbl_A,JLabel lbl_B
								,JLabel lbl_C,JLabel lbl_D)
	{		
		if(num_question  > 14)
		{			
			real_score = Score[num_score]; 
			JOptionPane.showMessageDialog(null, "Chuc Mung Ban , Ban La Nguoi Thang Cuoc! Diem : " + real_score );
			num_question = 0;
			return;
		}
		else
		{
			lbl_Num_Question.setText("Cau Hoi " + (num_question+1));
			lbl_Question.setText(questions[num_question].NoiDung);
			lbl_A.setText("A. " + questions[num_question].DapAnA);
			lbl_B.setText("B. " + questions[num_question].DapAnB);
			lbl_C.setText("C. " + questions[num_question].DapAnC);
			lbl_D.setText("D. " + questions[num_question].DapAnD);
		}
	}
	
	public boolean Test_Answer(int id,JLabel lbl_Num_Question,JLabel lbl_Question,
							JLabel lbl_A,JLabel lbl_B,JLabel lbl_C,JLabel lbl_D )
	{
		int buttondialog = JOptionPane.YES_NO_OPTION;
		if(JOptionPane.showConfirmDialog(null,"Day La Cau Tra Loi Cuoi Cung Cua Ban","Chot Dap An",buttondialog) == JOptionPane.YES_OPTION)
		{
			if(questions[num_question].DapAnDung == id)
			{				
				JOptionPane.showMessageDialog(null, "Dap An Dung");
				num_score = num_score + 1;
				if(num_score < 14) 
					real_score = Score[num_score + 1];
				num_question = num_question +1 ;	
				Question_Output(num_question,lbl_Num_Question,lbl_Question,lbl_A,lbl_B,lbl_C,lbl_D);
				Change_Color(lbl_A, 1);
				Change_Color(lbl_B, 1);
				Change_Color(lbl_C, 1);
				Change_Color(lbl_D, 1);
				id_answer = 0;
				return true;
			}
			else
			{
				if(choose_again == false)
				{
					real_score = Score[num_score];
					JOptionPane.showMessageDialog(null, "Sai, Ban Da Thua Cuoc! So diem ban dat duoc la " + real_score);	
					System.exit(0);
				}
				else
				{
					Help_Choose_Again(false);
					JOptionPane.showMessageDialog(null, "Dap An Sai , Xin Chon Lai");					
				}
			}
			return false;
		}
		if(buttondialog == JOptionPane.NO_OPTION)
			return false;
		return false;
	}
	
	public void Choose_Question_Color(JLabel lbl_choose , JLabel lbl_1 , JLabel lbl_2 , JLabel lbl_3)
	{
		Change_Color(lbl_choose, 0);
		Change_Color(lbl_1,1);
		Change_Color(lbl_2,1);
		Change_Color(lbl_3,1);
	}
	
	private void Change_Color(JLabel lbl, int flag)
	{
		if(flag == 0)
		{
			lbl.setBackground(Color.RED);
			flag = 1;
		}
		else
		{
			lbl.setBackground(Color.GREEN);
			flag = 0;
		}
	}

	public void Help_Change_Question (JLabel lbl_Num_Question,
			JLabel lbl_Question,JLabel lbl_A,JLabel lbl_B
			,JLabel lbl_C,JLabel lbl_D)
	{
		Question temp;
		temp = questions[num_question] ;
		questions[num_question] = questions[16];
		questions[16] = temp;
		Question_Output(num_question,lbl_Num_Question,lbl_Question,lbl_A,lbl_B,lbl_C,lbl_D);
		Change_Color(lbl_A, 1);
		Change_Color(lbl_B, 1);
		Change_Color(lbl_C, 1);
		Change_Color(lbl_D, 1);
	}
	
	public void Help_Choose_Again(boolean signal)
	{
		choose_again = signal;
	}
	
	public void Help_3()
	{
		Help_3 help3 = new Help_3(questions[num_question]);	
	}
}
